let utils = require("../../utils/util.js");
const heights = []
const weights = [];
let that;
for (let i = 10; i <= 300; i++) {
  heights.push(i);
  weights.push(i);
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    date: '1990-01-01',
    types: ['减脂', '塑形'],
    calorieType: "减脂",
    bodyType: "偏肥",
    bmi: "22",
    height: "199",
    heights: heights,
    weight: "50",
    weights: weights,
    liverRate:"1",
    gender: "男",

    pageStep: 1,
    waist: "",
    waistType: "",
    bmiNormalRange: {
      min: "",
      max: "",
    },
    waistNormalRange: {
      min: "",
      max: "",
    },
    animationPageOne:{},
    animationPageTwo:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let date = wx.getStorageSync("date") || "1990-01-01";
    let calorieType = wx.getStorageSync("calorieType") || "减脂";
    let height = wx.getStorageSync("height") || "";
    let weight = wx.getStorageSync("weight") || "";
    let bodyType = wx.getStorageSync("bodyType") || '待定';
    let gender = wx.getStorageSync("user").gender == 'female' ? '女' : '男';
    let bmi = wx.getStorageSync("bmi") || 0;
    let waist = wx.getStorageSync("waist") || "";
    let waistType = wx.getStorageSync("waistType");
    let liverRate = wx.getStorageSync("liverRate");
    if (height && weight) {
      bmi = parseInt(weight / (height / 100) / (height / 100));
    }

    const animationPageOne = wx.createAnimation({
      duration: 400,
      timingFunction: 'ease-out',
    });
    const animationPageTwo = wx.createAnimation({
      duration: 400,
      timingFunction: 'ease-out',
    });
    this.animationPageOne = animationPageOne;
    this.animationPageTwo = animationPageTwo;
    this.setData({
      date: date,
      calorieType: calorieType,
      height: height,
      weight: weight,
      waist: waist,
      gender: gender,
      bmi: bmi,
      bodyType: bodyType,
      waistType: waistType,
      liverRate: liverRate,
      animationPageOne: {},
      animationPageTwo: {}
    });
    that = this;

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindChange: function(e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]]
    })
  },
  bindPickerChange: function(e) {
    this.setData({
      calorieType: that.data.types[e.detail.value]
    })
  },
  bindHeightPickerChange: function(e) {
    this.setData({
      height: e.detail.value
    });
    this.checkBMI();
  },
  checkBMI() {
    if (this.data.weight && this.data.height) {
      let bmi = parseInt(this.data.weight / (this.data.height / 100) / (this.data.height / 100));
      if (bmi > 100 || bmi < 5 || !bmi) {
        bmi = "0";
      }
      let bodyType = "";
      if (bmi >= 18 && bmi <= 24) {
        bodyType = "正常";
      } else if (bmi < 18) {
        bodyType = "偏瘦";
      } else if (bmi >= 28) {
        bodyType = "肥胖";
      }
      this.setData({
        bmi: bmi,
        bodyType: bodyType
      });
    }
  },
  bindWeightPickerChange(e) {
    this.setData({
      weight: e.detail.value
    });
    this.checkBMI();
  },
  bindLiverRatePickerChange(e) {
    this.setData({
      liverRate: e.detail.value
    });
  },
  goToStep(e) {
    let step = e.currentTarget.dataset.step;
    if (step == 2) {
      that.calculateInfo();
    }
  },
  calculateInfo() {
    let date = this.data.date;
    let calorieType = this.data.calorieType;
    let height = +this.data.height;
    let weight = +this.data.weight;
    let bmi = +this.data.bmi;
    let bodyType = this.data.bodyType;
    let waist = +this.data.waist;
    let gender = this.data.gender;
    let liverRate = this.data.liverRate;
    if (!that.checkField(['height', 'weight', 'waist'])) return;

    if (height <= 0 || height > 200) {
      wx.showToast({
        title: '身高输入不正确',
        icon: "none"
      });
      return;
    }

    if (weight > 200 || weight <= 0) {
      wx.showToast({
        title: '体重输入不正确',
        icon: "none"
      });
      return;
    }
    if (waist > 200 || waist <= 0) {
      wx.showToast({
        title: '腰围输入不正确',
        icon: "none"
      });
      return;
    }

    // 计算卡路里需求等级
    let age = 0;
    let ageNowDate = new Date();
    let birthDate = new Date(date);
    age = Math.round((ageNowDate.getTime() - birthDate.getTime()) / 24 / 1000 / 60 / 60 / 365);
    let calorieValue = utils.getTotalCalorByAge({
      age,
      height,
      gender,
      BMI: bmi,
      waist
    });
    console.log(calorieValue);

    that.setData({
      bmiNormalRange: {
        min: 18.5 * 5,
        max: 24 * 5
      },
      bmiFullHeight: 40.5 * 5,
    });

    wx.setStorageSync("date", date);
    wx.setStorageSync("calorieType", calorieType);
    wx.setStorageSync("height", height);
    wx.setStorageSync("weight", weight);
    wx.setStorageSync('bodyType', bodyType);
    wx.setStorageSync('bmi', bmi);
    wx.setStorageSync('waist', waist);
    wx.setStorageSync('calorie', calorieValue || 1400);
    wx.setStorageSync('liverRate', liverRate);

    that.animateToPage(1,2);
  },
  animateToPage(f, t){
    let animationPageOne = that.animationPageOne;
    let animationPageTwo = that.animationPageTwo;
    if(t == '2'){
      animationPageOne.translateY("-100vh").step();
      animationPageTwo.translateY("0").step();
    }else if(t == '1'){
      animationPageOne.translateY("0").step();
      animationPageTwo.translateY("100vh").step();
    }
    this.setData({
      animationPageOne: animationPageOne.export(),
      animationPageTwo: animationPageTwo.export()
    });
  },
  bindAnimateToPage(e){
    let f = e.target.dataset.f;
    let t = e.target.dataset.t;
    this.animateToPage(f, t);
  },
  navigateBack() {
    wx.navigateBack({
      delta: 1,
    })
  },
  checkField(fields) {
    for (var item of fields) {
      if (that.data[item] && !isNaN(that.data[item])) continue;
      wx.showToast({
        title: '信息填写不正确',
        icon: "none",
        duration: 800
      });
      return false;
    }
    return true;
  },
  bindWaistPickerChange(e) {
    let waist = e.detail.value;
    if (that.data.height && that.data.gender) {
      // 计算腰围正常范围
      let waistPercent = "";
      let height = that.data.height,
        gender = that.data.gender;
      if (gender == '男') {
        waistPercent = height / 2 - 11;
      } else {
        waistPercent = height / 2 - 14;
      }
      let waistType = waist > waistPercent * 1.05 ? '偏大' : (waist < waistPercent * 0.95 ? '偏小' : '正常');
      that.setData({
        waistNormalRange: {
          min: waistPercent * 0.95,
          max: waistPercent * 1.05
        },
        waistFullHeight: waistPercent * 2,
        waistPercent: waistPercent,
        waistType: waistType
      });
      wx.setStorageSync("waistType", waistType);
    }

    this.setData({
      waist: e.detail.value,
    })
  }
})